package com.freek.ScreenDrop;
import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.prefs.Preferences;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;

import com.dropbox.client2.exception.DropboxException;


public class Main
{
	public static TrayIcon trayIcon;
	public static void main(String[] args)
	{
		
		EventQueue.invokeLater(new Runnable() {
			public void run()
			{
				try
				{
					//use a more native look and feel, one that isn't hideous
					try
					{
						UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					} catch (ClassNotFoundException | InstantiationException
							| IllegalAccessException | UnsupportedLookAndFeelException e1)
					{
						e1.printStackTrace();
					}
					
					try
					{
						Authenticator.authenticate();
					} catch (DropboxException | IOException
							| URISyntaxException e2)
					{
						e2.printStackTrace();
					}
					
					final OptionsFrame frame = new OptionsFrame();
					frame.setVisible(false);
					
					if(!SystemTray.isSupported())
					{
						System.err.println("The System Tray is not supported.");
						if(GlobalScreen.isNativeHookRegistered())
							GlobalScreen.unregisterNativeHook();
						System.exit(1);
					}
					SystemTray sysTray = SystemTray.getSystemTray();
					
					URL url = getClass().getResource("img/icon.png");
					Image icon = Toolkit.getDefaultToolkit().getImage(url);
					
					
					PopupMenu popup = new PopupMenu();
					MenuItem menuExit = new MenuItem("Exit");
					menuExit.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent e)
						{
							if(GlobalScreen.isNativeHookRegistered())
								GlobalScreen.unregisterNativeHook();
							System.exit(0);
						}
					});
					popup.add(menuExit);
					
					MenuItem menuCapDesktop = new MenuItem("Capture Desktop");
					menuCapDesktop.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent e)
						{
							try
							{
								Capture.desktop();
							} catch (IOException | AWTException | DropboxException | URISyntaxException e1)
							{
								e1.printStackTrace();
							}
						}
					});
					popup.add(menuCapDesktop);
					
					MenuItem menuCapRegion = new MenuItem("Capture Region");
					menuCapRegion.addActionListener(new ActionListener() {
						
						@Override
						public void actionPerformed(ActionEvent e)
						{
							final RegionSelectFrame frame = RegionSelectFrame.selectRegion();
							frame.addWindowListener(new WindowAdapter() {
								public void windowClosed(WindowEvent e) {
									try
									{
										if(frame.region != null)
											Capture.region(RegionSelectFrame.absRect(frame.region));
									} catch (IOException | AWTException e1)
									{
										e1.printStackTrace();
									}
								};
							});
						}
					});
					popup.add(menuCapRegion);
					
					trayIcon = new TrayIcon(icon, "ScreenDrop", popup);
					trayIcon.setImageAutoSize(true);
					sysTray.add(trayIcon);
					trayIcon.addMouseListener(new MouseAdapter(){
						@Override
						public void mouseClicked(MouseEvent e)
						{
							if(e.getButton() == MouseEvent.BUTTON1)
								frame.setVisible(true);
						}
					});
					
					
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
		
		
		try
		{
			GlobalScreen.registerNativeHook();
			GlobalKeyListener keyListener = new GlobalKeyListener(); 
			GlobalScreen.getInstance().addNativeKeyListener(keyListener);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
}
